<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>C++ Stacks</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Stacks</a>
  </div>

  <div class="name-format">
    empty
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stack&gt;
  bool empty() const;
</pre>

  <p>The empty() function returns true if the stack has no elements,
  false otherwise.</p>

  <p>For example, the following code uses empty() as the stopping
  condition on a (C/C++ Keywords) <a href=
  "../keywords/while.html">while</a> loop to clear a stack and display
  its contents in reverse order:</p>
  <pre class="example-code">
 vector&lt;int&gt; v;
 for( int i = 0; i &lt; 5; i++ ) {
   v.push_back(i);
 }
 while( !v.empty() ) {
   cout &lt;&lt; v.back() &lt;&lt; endl;
   v.pop_back();
 }              
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="size.html">size</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    pop
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stack&gt;
  void pop();
</pre>

  <p>The function pop() removes the top element of the stack and
  discards it.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    (C++ Priority Queues) <a href=
    "../cpppriority_queue/push.html">push</a><br>
    <a href="top.html">top</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    push
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stack&gt;
  void push( const <a href="../containers.html">TYPE</a>&amp; val );
</pre>

  <p>The function push() adds <em>val</em> to the top of the current
  stack.</p>

  <p>For example, the following code uses the push() function to add
  ten integers to the top of a stack:</p>
  <pre class="example-code">
   stack&lt;int&gt; s;
   for( int i=0; i &lt; 10; i++ )
     s.push(i);         
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="pop.html">pop</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    size
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stack&gt;
  <strong>size_type</strong> size() const;
</pre>

  <p>The size() function returns the number of elements in the current
  stack.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    (C++ Strings) <a href="../cppstring/capacity.html">capacity</a><br>
    <a href="empty.html">empty</a><br>
    (C++ Strings) <a href="../cppstring/length.html">length</a><br>
    (C++ Multimaps) <a href=
    "../cppmultimap/max_size.html">max_size</a><br>
    (C++ Strings) <a href="../cppstring/resize.html">resize</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    Stack constructors
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stack&gt;
  stack();
  stack( const Container&amp; con );
</pre>

  <p>Stacks have an empty constructor and a constructor that can be
  used to specify a container type.</p>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    top
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stack&gt;
  <a href="../containers.html">TYPE</a>&amp; top();
</pre>

  <p>The function top() returns a reference to the top element of the
  stack.</p>

  <p>For example, the following code removes all of the elements from a
  stack and uses top() to display them:</p>
  <pre class="example-code">
   while( !s.empty() ) {
     cout &lt;&lt; s.top() &lt;&lt; &quot; &quot;;
     s.pop();
   }            
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="pop.html">pop</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>
</body></html>
